﻿namespace UI
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    using Common.Helpers;
    using Common.Protocol;

    public partial class ChannelSwitcher : UserControl
    {
        private List<SwitcherCell> cells = new List<SwitcherCell>();

        private readonly int rowsCount = 4, colsCount = 8;

        public event EventHandler ChannelStateSwitched;

        public ChannelSwitcher()
        {
            this.InitializeComponent();
            this.InitCells();
        }

        public void InitCells()
        {
            int labelWidth = this.Width / this.colsCount;
            int labelHeight = this.Height / this.rowsCount;
            int counter = 0;

            for (int i = 0; i < this.rowsCount; i++)
            {
                for (int j = 0; j < this.colsCount; j++)
                {
                    SwitcherCell label = new SwitcherCell();
                    label.BorderStyle = BorderStyle.FixedSingle;
                    label.Text = Channel.GetIdFromNumber(counter);
                    label.Width = labelWidth;
                    label.Height = labelHeight;
                    label.Top = labelHeight * i;
                    label.Left = labelWidth * j;
                    label.Enabled = false;
                    label.Click += this.CellClickEventHandler;
                    this.cells.Add(label);
                    this.Controls.Add(label);
                    counter++;
                }
            }
        }

        public void Initialize(Block currentBlock)
        {
            foreach (var cell in this.cells)
            {
                cell.Disable();
            }

            foreach (var channel in currentBlock.Channels)
            {
                SwitcherCell cell = this.cells.Find(l => l.Text == channel.Id);

                if (cell != null)
                {
                    cell.Initialize(channel);
                    cell.Enabled = true;
                    cell.IsOn = ChannelDispatcher.IsActive(cell.Text);
                }
            }
        }

        private void CellClickEventHandler(object sender, EventArgs e)
        {
            var cell = sender as SwitcherCell;
            string id = cell.Text;
            if (ChannelDispatcher.IsActive(id))
            {
                ChannelDispatcher.Deactivate(id);
                cell.IsOn = false;
            }
            else
            {
                ChannelDispatcher.Activate(id);
                cell.IsOn = true;
            }

            if (this.ChannelStateSwitched != null)
            {
                this.ChannelStateSwitched(this, null);
            }
        }

        private void ChannelSwitcherResizeEventHandler(object sender, EventArgs e)
        {
            try
            {
                int labelWidth = this.Width / this.colsCount;
                int labelHeight = this.Height / this.rowsCount;
                int counter = 0;

                for (int i = 0; i < this.rowsCount; i++)
                {
                    for (int j = 0; j < this.colsCount; j++)
                    {
                        SwitcherCell cell = this.cells[counter];
                        cell.Width = labelWidth;
                        cell.Height = labelHeight;
                        cell.Top = labelHeight * i;
                        cell.Left = labelWidth * j;
                        counter++;
                    }
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                
            }
        }
    }
}